<template>
  <div class="check-task">
    <div>
        <van-field
        class="input_text"
        v-model="data.DangerOperateTypeName"
        label="危险作业类型"
        placeholder="请选择"
        required
        readonly
        @click="SitdanName()"
      >
        <template #right-icon><van-icon name="arrow" /></template>
      </van-field>

	      <date-picker
        label="上报日期"
        required
        :date.sync="data.ApplyDate"
      ></date-picker>


      <van-field label="位置" v-model="data.Location" placeholder="请填写" >
      </van-field>


      <van-field
        class="input_text"
        v-model="data.OperateObjName"
        label="作业方"
        placeholder="请选择"
        required
        readonly
        @click="SitridoName()"
      >
        <template #right-icon><van-icon name="arrow" /></template>
      </van-field>


      <!-- 日期 Date-->
      <date-picker
        label="开始时间"
        required
        :date.sync="data.StartDate"
      ></date-picker>
      <date-picker
        label="结束时间"
        required
        :date.sync="data.EndDate"
      ></date-picker>

	  <van-field label="作业人员" v-model="data.OperateChnName" >
      </van-field>

	  <van-field label="监护人员" v-model="data.WatchMan" >
      </van-field>

	  <FileUpload
        @CallBack="_fileUploadCb"
        :attachs="data.Attachs"
        :labelName="'施工作业记录'"
        :allowEdit="true"
      />
    
      <div class="bottom-btns">
        <van-button class="submit-btn" type="primary" @click="save(true)">
          提交
        </van-button>
      </div>
    </div>
    
    <SitdangerType :show.sync="show" :data="data" @cb="close" />

    <SitridoType :show.sync="ridoshow" :data="data" @dg="ridoclose" />
  </div>
</template>

<script>
import {
  sitgetDetail,
  Editsumit,
  sitInitNewEntity,
} from "@/api/SitDangerOperateApplyrecordNew";
import DatePicker from "../../../components/DatePicker.vue";
import store from "@/store/index";
import SitdangerType from "../SiteDangerOperateApplyRecord/SitdangerType";
import SitridoType from "../SiteDangerOperateApplyRecord/SitridoType";
import FileUpload from "../../../components/Attach/FileUploadYat.vue";

export default {
  components: { 
    DatePicker,
    SitdangerType,
    SitridoType,
	  FileUpload
  },
  data() {
    return {
      data: {},
      show: false,
      ridoshow: false,
      filter: {
        id: this.$route.query.id,
      },
      params: [
        {
        },
      ],
      listUser: [],
      listCheckTable: [],
      chooseUserUrl: this.$constUrl.CHOOSE_USER,
      userTagData: [],
    };
  },
  created() {
    if (this.filter.id > 0) {
      store.commit("updateTitle", "危险作业上报记录[编辑]");
      this.Sitdangerdetail();
    } else {
      this.sitInitNewEntity();
      store.commit("updateTitle", "危险作业上报记录[新增]");
    }
  },
  methods: {
    _initListUser(list) {
      this.listUser = list;
    },
    _initListChcekTable(list) {
      this.listCheckTable = list;
    },

    _fileUploadCb(attachs) {
			this.data.Attachs = attachs;
		},

    // 新增初始化接口
    sitInitNewEntity() {
      sitInitNewEntity(this.filter).then((res) => {
        this.data = res.data;
      });
    },

    // 编辑详情接口
    Sitdangerdetail() {
      sitgetDetail(this.filter).then((res) => {
        this.data = res.data;
        res.data.DangerOperateTypeList.forEach((item) => {
            if (res.data.DangerOperateType == item.ID) {
              res.data.DangerOperateType = item.ID;
              res.data.DangerOperateTypeName = item.Name;
            }
          });
          this.SitdangerType  = res.data.DangerOperateType
         res.data.OperateObjList.forEach((item) => {
            if (res.data.OperateObj == item.ID) {
              res.data.OperateObj = item.ID;
              res.data.OperateObjName = item.Name;
            }
          });
          this.SitridoType  = res.data.OperateObj
      });
    },

    // 选择危险作业类型
    SitdanName() {
      this.show = true;
    },
    
    // 选择危险作业类型
    close(type) {
      if (type) {
          this.data.DangerOperateTypeList.forEach((item) => {
            if (item.ID == type) {
              this.data.DangerOperateType = item.ID;
              this.data.DangerOperateTypeName = item.Name;
            }
          });
          this.SitdangerType  = this.data.DangerOperateType
      }

      this.show = false;
    },
    // 选择作业方类型
    SitridoName() {
      this.ridoshow = true;
    },
    
    // 选择作业方类型
    ridoclose(type) {
      if (type) {
          this.data.OperateObjList.forEach((item) => {
            if (item.ID == type) {
              this.data.OperateObj = item.ID;
              this.data.OperateObjName = item.Name;
            }
          });
          this.SitridoType  = this.data.OperateObj
      }

      this.ridoshow = false;
    },

    save() {
      if (!this.data.DangerOperateType  ) {
        return this.$toast("请选择危险作业类型");
      }
      if (!this.data.ApplyDate) {
        return this.$toast("请选择申请时间");
      }
       if (!this.data.OperateObjName) {
        return this.$toast("请选择作业方");
      }
      if (!this.data.StartDate) {
        return this.$toast("请选择开始时间");
      }
      if (!this.data.EndDate) {
        return this.$toast("请选择结束时间");
      }
      this.loading = true;
      let param = {
        ID: this.data.ID,
        DangerOperateType : this.data.DangerOperateType,
        DangerOperateTypeName : this.data.DangerOperateTypeName,
        ApplyDate : this.data.ApplyDate,
        Attachs : this.data.Attachs,
        StartDate : this.data.StartDate,
        EndDate : this.data.EndDate,
        Location : this.data.Location,
        OperateObj : this.data.OperateObj,
        OperateObjName : this.data.OperateObjName,
        OperateChnName : this.data.OperateChnName,
        WatchMan : this.data.WatchMan,
      };
      
      if (this.data.ID) {
        Editsumit(param)
          .then((res) => {
            this.$toast("保存成功");
            this.loading = false;
            setTimeout(() => {
              this.$router.goRefresh();
            }, 500);
          })
          .catch((err) => {
            console.log("err", err);
          });
      }else{
         Editsumit(param)
          .then((res) => {
            this.$toast("保存成功");
            this.loading = false;
            setTimeout(() => {
              this.$router.goRefresh();
            }, 500);
          })
          .catch((err) => {
            console.log("err", err);
          });
      }
    },
  },
};
</script>

<style lang="less" scoped>
.check-task {
  background-color: #e8f1f8;
  padding-bottom: 20px;
}
.bottom-btns {
  margin-top: 10px;
  padding: 0 10px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  .submit-btn {
    width: 100%;
    height: 35px;
    font-size: 14px;
  }
}

.det_tab_item {
  padding: 10px 0;
  background-color: #fff;
  border-radius: 5px;
  margin-bottom: 10px;

  .det_tab_item_text {
    padding: 0 10px;
  }
}
.operate_det {
  background-color: #fff;
  padding: 10px;
  .operate_list {
    font-size: 12px;
    border: 1px solid #e8f1f8;
    .operate_row_first {
      padding: 10px;
      background-color: #e8f1f8;
      border-bottom: 1px solid #e8f1f8;
      margin-bottom: 10px;
    }
    .operate_row {
      padding: 10px;
      border-bottom: 1px solid #e8f1f8;
      /*margin-bottom: 10px;*/
    }
  }
}
.operation-imgs {
  width: 15px;
  height: 15px;
  vertical-align: middle;
  margin-right: 5px;
}

.risk_item_radio {
  width: 100%;
  background-color: #fff;
  display: flex;
  justify-content: space-between;
  align-items: center;
  // border-bottom: 1px solid #e8f1f8;
}
</style>
